package com.keba.lamp.complaint.service.impl;

import com.google.common.collect.Multimap;
import com.keba.basic.database.mybatis.conditions.Wraps;
import com.keba.basic.database.mybatis.conditions.query.QueryWrap;
import com.keba.basic.utils.BeanPlusUtil;
import com.keba.lamp.common.constant.AppendixType;
import com.keba.lamp.file.entity.Appendix;
import com.keba.lamp.file.service.AppendixService;
import com.keba.lamp.file.service.FileService;
import com.keba.lamp.file.vo.result.FileResultVO;
import com.keba.lamp.model.vo.result.AppendixResultVO;
import com.keba.lamp.model.vo.save.AppendixSaveVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.keba.lamp.complaint.service.ChannelCustomerService;
import com.keba.basic.base.service.impl.SuperServiceImpl;
import com.keba.lamp.complaint.manager.ChannelCustomerManager;
import com.keba.lamp.complaint.entity.ChannelCustomer;
import com.keba.lamp.complaint.vo.save.ChannelCustomerSaveVO;
import com.keba.lamp.complaint.vo.update.ChannelCustomerUpdateVO;
import com.keba.lamp.complaint.vo.result.ChannelCustomerResultVO;
import com.keba.lamp.complaint.vo.query.ChannelCustomerPageQuery;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 * 业务实现类
 * 渠道客户
 * </p>
 *
 * @author twg
 * @date 2025-04-15 23:21:22
 * @create [2025-04-15 23:21:22] [twg] [代码生成器生成]
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class ChannelCustomerServiceImpl extends SuperServiceImpl<ChannelCustomerManager, Long, ChannelCustomer> implements ChannelCustomerService {
    private final AppendixService appendixService;

    private final FileService fileService;

    @Override
    public <SaveVO> ChannelCustomer save(SaveVO saveVO) {
        ChannelCustomerSaveVO channelCustomerSaveVO = (ChannelCustomerSaveVO) saveVO;
        ChannelCustomer channelCustomer = super.saveBefore(saveVO);
        superManager.save(channelCustomer);
        // 保存图片
        appendixService.save(AppendixSaveVO.build(channelCustomer.getId(), AppendixType.Complaint.COMPLAINT__CHANNEL__CUSTOMER__IMAGE, channelCustomerSaveVO.getAppendixIcon()));
        return channelCustomer;
    }

    @Override
    public <UpdateVO> ChannelCustomer updateById(UpdateVO updateVO) {
        ChannelCustomerUpdateVO channelCustomerUpdateVO = (ChannelCustomerUpdateVO) updateVO;
        ChannelCustomer channelCustomer = super.updateById(updateVO);
        appendixService.save(AppendixSaveVO.build(channelCustomer.getId(), AppendixType.Complaint.COMPLAINT__CHANNEL__CUSTOMER__IMAGE, channelCustomerUpdateVO.getAppendixIcon()));
        return channelCustomer;
    }


    @Override
    public List<ChannelCustomerResultVO> list(ChannelCustomerPageQuery query) {
        ChannelCustomer entity = BeanPlusUtil.toBean(query, getEntityClass());
        QueryWrap<ChannelCustomer> wrapper = Wraps.q(entity);
        List<ChannelCustomer> list = list(wrapper);
        List<ChannelCustomerResultVO> customerResultVOS = BeanPlusUtil.toBeanList(list, ChannelCustomerResultVO.class);
        customerResultVOS.forEach(o ->{
            Appendix appendix = appendixService.getOne(Wraps.<Appendix>lbQ().eq(Appendix::getBizId, o.getId()));
            if (Objects.nonNull(appendix)) {
                o.setFileResult(BeanPlusUtil.toBean(fileService.getById(appendix.getId()),FileResultVO.class));
            }
        });
        return customerResultVOS;
    }
}


